<template>
	<view class="each">
		<view class="each-inpt">
			<input v-model="value" type="text">
		</view>
		<view style="height:90rpx;"></view>
		<view class="each-cont" v-if="value.length">
			<view v-for="item in Listcon" @click="xqing(item)">
				<view>{{item.goods_name}}</view>
				<view>></view>
			</view>
		</view>
		<view class="each-hitry" v-else>
			<view>
				<view>搜历史</view>
				<view @click="open">删除</view>
			</view>
			<view>
				<view v-for="item in his" @click="value = item">{{item}}</view>	
			</view>
		</view>
	</view>
	<uni-popup ref="popup" type="dialog">
		<uni-popup-dialog mode="base" title='您确定要删浏览记录吗？' :duration="2000" :before-close="true" @close="close" @confirm="confirm"></uni-popup-dialog>
	</uni-popup>
</template>

<script>
	export default {
		data() {
			return {
				value:'',
				Listcon:[],
				fdong:null,
				his:[]
			};
		},
		watch:{
			value(n){
				this.ajx(n)	
			}
		},
		methods:{
			ajx(n){
				let th = this;
				clearTimeout(th.fdong);
				if(this.value != 0){
					th.fdong = setTimeout(()=>{
						uni.request({
							url:`https://www.uinav.com/api/public/v1/goods/qsearch?query=${n}`,
							success(res) {
								th.Listcon = res.data.message;
								const set = new Set(th.his);
								set.delete(n);
								th.his = Array.from(set);
								th.his.unshift(n);
							}
						})
					},500)
				}else{
					th.Listcon = [];
				}
			},
			xqing(n){
				uni.navigateTo({
					url: "/subpkg/goods/goods?id="+n.goods_id
				})
			},
			open() {
				this.$refs.popup.open()
			},
			close() {
				// TODO 做一些其他的事情，before-close 为true的情况下，手动执行 close 才会关闭对话框
				// ...
				this.$refs.popup.close();
			},
		
			confirm(value) {
				// 输入框的值
				this.his = []
				// TODO 做一些其他的事情，手动执行 close 才会关闭对话框
				// ...
				this.$refs.popup.close()
			}
		}
	}
</script>

<style lang="scss">
	.each{
		>.each-inpt{
			width:100%;
			height:90rpx;
			margin:0 auto;
			padding-top: 10rpx;
			box-sizing: border-box;
			position: fixed;
			top:0;
				background-color: #ff0000;
				>input{
					width:90%;
					height:70rpx;
					background-color: white;
					border-radius: 80rpx;
					outline: none;
					padding-left:20rpx;
					margin:0 auto;
				}
		}
		>.each-cont{
			width:90%;
			margin:0 auto;
			height: 80rpx;
			>view{
				width:100%;
				height: 80rpx;
				display: flex;
				align-items: center;
				justify-content: space-between;
				>view:nth-child(1){
					flex:1;
					height: 80rpx;
					line-height:80rpx;
					text-overflow: ellipsis;
					white-space: nowrap;
					overflow: hidden;
				}
				>view:nth-child(2){
					width:40rpx;	
					text-align: right;
				}
			}
		}
		>.each-hitry{
			width:90%;
			margin:0 auto;
			>view:nth-child(1){
				height:80rpx;
				display: flex;
				justify-content: space-between;
				align-items: center;
				>view{
					font-size: 30rpx;
					font-weight: 600;
				}
			}
			>view:nth-child(2){
				display: flex;
				flex-wrap: wrap;
				>view{
					border:1px gray solid;
					padding:0 15rpx 10rpx 15rpx;
					margin:10rpx;
				}
			}
			
		}	
	}
</style>
